


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>树形组件</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiAdmin.std/src/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiAdmin.std/src/layuiadmin/style/admin.css" media="all">
</head>
<body>


<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-header">分类结构</div>
                <div class="layui-card-body">
                    <div class="layui-btn-container">
                        <button type="button" class="layui-btn layui-btn-sm" onclick="add()">新增分类</button>
                    </div>
                    <div id="tree_category"></div>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-header">详细信息</div>
                <div class="layui-card-body">

                    <div class="layui-form">

                        <div class="layui-form-item">
                            <label class="layui-form-label">上级分类：</label>
                            <div class="layui-input-block">
                                <input type="hidden" id="id" name="id" value="0" />
                                <input type="hidden" id="pid" name="pid" value="0" />
                                <input type="text" id="pname" name="pname" readonly
                                       lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类名称：</label>
                            <div class="layui-input-block">
                                <input type="text" id="title" name="title" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">排序值：</label>
                            <div class="layui-input-block">
                                <input type="text" id="idx" name="idx" lay-verify="required" value="0"
                                       placeholder="" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="btn_save">保存编辑</button>
                                <input type="button" class="layui-btn layui-btn-danger" value="删除分类" onclick="del()" id="btn_del" style="display: none">
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="/static/layuiAdmin.std/src/layuiadmin/layui/layui.js"></script>
<script>
    var tree;
    var layer;
    var util;
    var select;
    var $;
    var form;

    //模拟数据
    var tree_data = []

    function save(field) {
        $.ajax({
            url: 'save',
            type: 'POST',
            data: field,
            dataType: 'json',
            success: function (data) {
                if (data.code == '0') {
                    layer.msg('保存成功', { icon: 1, time: 2000 }, function () {
                        location.reload()
                    });

                } else {
                    layer.msg(data.msg, { icon: 5, time: 2000 });
                }
            }
        });
    }

    function add() {
        $('#id').val(0);
        $('#pid').val(0);
        $('#pname').val('');
        $('#title').val('');
        $('#idx').val('0');

        $('#btn_del').hide()
        $('#btn_save').val('新增分类')

        layer.msg('请在右侧表单填写内容')
    }

    function del() {
        layer.confirm('确定删除分类？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url: 'del',
                type: 'POST',
                data: {
                    id: $('#id').val()
                },
                dataType: 'json',
                success: function (data) {
                    if (data.code == '0') {
                        location.reload()
                    } else {
                        layer.msg(data.msg, { icon: 5, time: 2000 });
                    }
                }
            });
        }, function(){

        });
    }

    function render_tree() {

        //树形结构
        tree.render({
            elem: '#tree_category'
            ,data: tree_data
            ,showLine: true
            //,showCheckbox: true  //是否显示复选框
            ,id: 'tree_category'
            //,isJump: true //是否允许点击节点时弹出新窗口跳转
            //,edit: ['add', 'update', 'del']
            ,click: function(obj){
                var data = obj.data;  //获取当前点击的节点数据
                //layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
                //alert(JSON.stringify(data))

                if (data.id == 0) {
                    return
                }

                $('#id').val(data.id);
                $('#pid').val(data.pid);
                $('#pname').val(data.pname);
                $('#title').val(data.title);
                $('#idx').val(data.idx);

                $('#btn_del').show()
                $('#btn_save').val('保存编辑')

            }
        });

        //树形分类
        select.render({
            el: 'pname',
            data: tree_data,
            type: 'radio',
            checked: function (obj) {
                console.log(JSON.stringify(obj))
                //obj.value 已选数据的title
                //obj.data 已选数据

                //alert(obj.obj.data.id)
                //alert(JSON.stringify(obj))

                $('#pid').val(obj.obj.data.id)
            }
        })
    }

    function load_tree_data() {
        $.ajax({
            url: 'load_category',
            type: 'POST',
            data: {

            },
            dataType: 'json',
            success: function (data) {
                if (data.code == '0') {
                    tree_data = data.tree
                    render_tree()
                } else {
                    layer.msg(data.msg, { icon: 5, time: 2000 });
                }
            }
        });
    }

    layui.config({
        base: '/static/layuiAdmin.std/src/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tree', 'util', 'customSelect'], function(){
        tree = layui.tree
        layer = layui.layer
        util = layui.util
        select = layui.customSelect
        $ = layui.$
        form = layui.form



        load_tree_data()


        form.on('submit(btn_save)', function(obj){

            save(obj.field)

        });
    });
</script>
</body>
</html>